package com.example.Config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description MQ配置类
 * @Author IT小辉同学
 * @Date 2023/06/16
 */
@Configuration
public class RabbitMQConfig {
    /**
     * @return {@link Queue }
     * @Description 设置队列
     * @Author IT小辉同学
     * @Date 2023/06/16
     */

    /**
     * 这是普通类型的消息发送与接收的配置
     */
//    @Bean
//    public Queue queue(){
//        return new Queue("simple.hello");
//    }

    /**
     * 这是广播模式（工作队列模式）
     *
     */
//    //队列1
//    private static final String QUEUE01 = "queue01";
//    //队列2
//    private static final String QUEUE02 = "queue02";
//    //交换机
//    private static final String EXCHANGE_NAME = "fanout_exchange";
//
//    @Bean
//    public Queue queue1() {
//        return new Queue(QUEUE01);
//    }
//
//    @Bean
//    public Queue queue2() {
//        return new Queue(QUEUE02);
//    }
//    //扇出交换机
//    @Bean
//    public FanoutExchange fanoutExchange() {
//        return new FanoutExchange(EXCHANGE_NAME);
//    }
//    //绑定  器
//    //binding01()和binding02()分别将queue1()和queue2()绑定到fanoutExchange()上，表示这两个队列都发送到广播交换机上。
//    @Bean
//    public Binding binding01() {
//        return BindingBuilder.bind(queue1()).to(fanoutExchange());
//    }
//
//    @Bean
//    public Binding binding02() {
//        return BindingBuilder.bind(queue2()).to(fanoutExchange());
//    }
    /**
     * 订阅模式、发布模式
     */
//    //队列01
//    private static final String QUEUE01= "queue01";
//    //队列02
//    private static final String QUEUE02= "queue02";
//    //交换机
//    private static final String EXCHANGE_NAME = "direct_exchange";
//    //路由键01
//    private static final String ROUTINGKEY01 = "queue_route01";
//    //路由键02
//    private static final String ROUTINGKEY02 = "queue_route02";
//
//    @Bean
//    public Queue queue01(){
//        return new Queue(QUEUE01);
//    }
//    @Bean
//    public Queue queue02(){
//        return new Queue(QUEUE02);
//    }
//    @Bean
//    public DirectExchange directExchange(){
//        return new DirectExchange(EXCHANGE_NAME);
//    }
//
//    @Bean
//    public Binding binding1(){
//        //将列队01绑定到交换机上为给他设置路由键
//        return BindingBuilder.bind(queue01()).to(directExchange()).with(ROUTINGKEY01);
//    }
//    @Bean
//    public Binding binding2(){
//        //将列队02绑定到交换机上为给他设置路由键
//        return BindingBuilder.bind(queue02()).to(directExchange()).with(ROUTINGKEY02);
//    }

    /**
     * 路由模式
     */

//    public static final String EXCHANGE_NAME = "direct_exchange";
//
//    public static final String QUEUE_NAME_1 = "queue_1";
//    public static final String QUEUE_NAME_2 = "queue_2";
//
//    public static final String ROUTING_KEY_1 = "key_1";
//    public static final String ROUTING_KEY_2 = "key_2";
//
//    @Bean
//    public DirectExchange directExchange() {
//        return new DirectExchange(EXCHANGE_NAME);
//    }
//
//    @Bean
//    public Queue queue1() {
//        return new Queue(QUEUE_NAME_1);
//    }
//
//    @Bean
//    public Queue queue2() {
//        return new Queue(QUEUE_NAME_2);
//    }
//
//    @Bean
//    public Binding binding1() {
//        return BindingBuilder.bind(queue1()).to(directExchange()).with(ROUTING_KEY_1);
//    }
//
//    @Bean
//    public Binding binding2() {
//        return BindingBuilder.bind(queue2()).to(directExchange()).with(ROUTING_KEY_2);
//    }

    /**
     * 主题模式
     */
    public static final String EXCHANGE_NAME = "topic_exchange";

    public static final String QUEUE_NAME_1 = "queue_1";
    public static final String QUEUE_NAME_2 = "queue_2";
    public static final String QUEUE_NAME_3 = "queue_3";

    public static final String ROUTING_KEY_1 = "key_1.*";
    public static final String ROUTING_KEY_2 = "key_2.*";
    public static final String ROUTING_KEY_3 = "*.key_3";

    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange(EXCHANGE_NAME);
    }

    @Bean
    public Queue queue1() {
        return new Queue(QUEUE_NAME_1);
    }

    @Bean
    public Queue queue2() {
        return new Queue(QUEUE_NAME_2);
    }

    @Bean
    public Queue queue3() {
        return new Queue(QUEUE_NAME_3);
    }

    @Bean
    public Binding binding1() {
        return BindingBuilder.bind(queue1()).to(topicExchange()).with(ROUTING_KEY_1);
    }

    @Bean
    public Binding binding2() {
        return BindingBuilder.bind(queue2()).to(topicExchange()).with(ROUTING_KEY_2);
    }

    @Bean
    public Binding binding3() {
        return BindingBuilder.bind(queue3()).to(topicExchange()).with(ROUTING_KEY_3);
    }

}


